package com.cskaoyan.controller.system;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.system.ObjDelBo;
import com.cskaoyan.bean.bo.system.ObjListBo;
import com.cskaoyan.bean.bo.system.ObjUpdateBo;
import com.cskaoyan.bean.vo.system.ObjCreateVo;
import com.cskaoyan.bean.vo.system.ObjListVo;
import com.cskaoyan.bean.vo.system.ObjUpdateVo;
import com.cskaoyan.service.system.StorageService;
import com.cskaoyan.util.common.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;

/**
 * @program: market
 * @description: 对象存储
 * @author: zhong
 * @create: 2022-07-17 18:10
 **/
@RestController
@RequestMapping("admin/storage")
public class StorageController {
    @Autowired
    StorageService storageService;

    /**
     * @param pageReg:页面规则
     * @return 对象存储页面信息
     * @auther zhong
     * @description admin/system/list 显示存储对象
     **/
    @GetMapping("list")
    public BaseRespVo list(ObjListBo pageReg) {
        ObjListVo objListVo = storageService.getObjListOp(pageReg);
        return BaseRespVo.ok(objListVo, "成功", 0);
    }

    /**
     * @param file:binary→上传对象字节文件
     * @return 成功返回对象的存储信息
     * @auther zhong
     * @description admin/system/create 新建存储对象
     */
    @PostMapping("create")
    public BaseRespVo create(MultipartFile file) throws IOException, ParseException {
        ObjCreateVo objCreateVo = storageService.addObjOp(file);
        return BaseRespVo.ok(objCreateVo, "成功", 0);
    }
    /**
     * @param delObj:删除对象
     * @return 成功返回信息
     * @auther zhong
     * @description admin/system/delete 删除存储对象
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody ObjDelBo delObj)  {
        int code = storageService.delObjOp(delObj.getId());
        if (code == Constant.FORBIDDEN_CODE) {
            return BaseRespVo.invalidData("对象已删除");
        }
        return BaseRespVo.ok();
    }
    /**
     * @param targetObj:更改对象
     * @return 成功返回信息
     * @auther zhong
     * @description admin/system/update 更改存储对象信息
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody ObjUpdateBo targetObj)  {
        ObjUpdateVo objUpdateVo = storageService.changeNameOp(targetObj.getName(), targetObj.getId());
        return BaseRespVo.ok(objUpdateVo);
    }

}
